package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class RateListActivity2 extends AppCompatActivity implements AdapterView.OnItemClickListener,AdapterView.OnItemLongClickListener {
    private static final String TAG = "RateListActivity2";
    ListView listview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rate_list2);

        //初始化数据项
        ArrayList<HashMap<String, String>> ListItems = new ArrayList<HashMap<String, String>>();
        for (int i=0;i<30;i++){
            HashMap<String, String> map = new HashMap<String,String>();
            map.put("ItemTitle","Rate:"+i);
            map.put("ItemDetail","Detail:"+i);
            ListItems.add(map);
        }
        //生成适配器的Item和 动态数组对应的元素
        SimpleAdapter ListItemAdapter = new SimpleAdapter(this,
                ListItems,
                R.layout.list_item,
                new String[]{"ItemTitle","ItemDetail"},
                new int[]{R.id.title,R.id.detail}

        );
        //绑定控件
        listview = findViewById(R.id.mylist2);
        listview.setAdapter(ListItemAdapter);
        listview.setOnItemClickListener(this);
        listview.setOnItemLongClickListener(this);

        Handler handler = new Handler(Looper.myLooper()){
            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what==7){
                    ArrayList<HashMap<String,String>> list = (ArrayList<HashMap<String, String>>) msg.obj;
                    //构造Adapter
                    SimpleAdapter ListItemAdapter = new SimpleAdapter(RateListActivity2.this,
                            list,
                            R.layout.list_item,
                            new String[]{"cname","cval"},
                            new int[]{R.id.title,R.id.detail}

                    );
                    listview.setAdapter(ListItemAdapter);
                }
                super.handleMessage(msg);
            }
        };
        MapTask task = new MapTask();
        task.setHandler(handler);

        Thread t = new Thread(task);
        t.start();

    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.i(TAG, "onItemClick: ");
        //获得数据项
        HashMap<String,String> map = (HashMap<String, String>) listview.getItemAtPosition(position);
        Log.i(TAG, "onItemClick: cname="+map.get("cname"));
        Log.i(TAG, "onItemClick: cval="+map.get("cval"));

//        String s1 = map.get("cname");
//        String s2 = map.get("cval");
//
//        Intent intent2 = new Intent(this,CalculateActivity.class);
//        intent2.putExtra("title",s1);
//        intent2.putExtra("detail",100f/Float.parseFloat(s2));
//
//        startActivity(intent2);
    }


    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        Log.i(TAG, "onItemLongClick: pos=" + position);
        return true;
    }
}